package compareTest;
import java.util.Arrays;

/**
 * @author 李泽伟
 */
public class Main {
    public static void main(String[] args) {
        //已实现Comparable接口的类
        String[] strS = new String[]{"华为", "荣耀", "d", "c"};
        Arrays.sort(strS);
        System.out.println(Arrays.toString(strS));
        //自定义类 未继承Comparable接口
        Goods[] goods = new Goods[4];
        goods[0] = new Goods("奥迪", 300, 20);
        goods[1] = new Goods("比亚迪", 100, 2000);
        goods[2] = new Goods("华为", 2000, 100);
        goods[3] = new Goods("荣耀", 2000, 40);
        // public class ClassCastException extends RuntimeException
        Arrays.sort(goods);
        System.out.println(Arrays.toString(goods));
    }
}
class Goods implements Comparable{
    private String name;
    private int price;
    private int sells;
    public Goods() {
    }
    public Goods(String name, int price, int sells) {
        this.name = name;
        this.price = price;
        this.sells = sells;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getSells() {
        return sells;
    }
    public void setSells(int sells) {
        this.sells = sells;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", sells=" + sells +
                '}';
    }
    @Override
    public int compareTo(Object o) {
        if(o instanceof Goods goods){
            if (this.price > goods.price){
                return 1;
            }else if (this.price < goods.price) {
                return -1;
            }else {
                return -this.name.compareTo(goods.name);
            }
            //return Integer.compare(this.price, goods.price);
        }
        throw new RuntimeException("参入参数不一致");
    }
}